﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.RegInfo.Models;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.RegInfo.Entities;

namespace BusinessLayer.RegInfo
{
    public class TransactionLogBL
    {
        public void Insert(int accountId, int subscriptionId, int typePay, int typeTransaction, string couponCode, string merchTxnRef, out double amount, out long transactionId, out int result)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@AccountId", accountId);
            adapter.AddParameter("@SubscriptionId", subscriptionId);
            adapter.AddParameter("@TypePay", typePay);
            adapter.AddParameter("@TypeTransaction", typeTransaction);
            adapter.AddParameter("@CouponCode", couponCode);
            adapter.AddParameter("@MerchTxnRef", merchTxnRef);
            var outputAmount = adapter.AddParameter("@Amount", SqlDbType.Float, ParameterDirection.Output);            
            var outputTransactionId = adapter.AddParameter("@TransactionId", SqlDbType.BigInt, ParameterDirection.Output);
            var outputResult = adapter.AddParameter("@Result", SqlDbType.Int, ParameterDirection.Output);
            adapter.ExecuteNonQuery("sp_TransactionLog_Insert", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            amount = (double)outputAmount.Value;
            transactionId = (long)outputTransactionId.Value;
            result = (int)outputResult.Value;
        }

        public void Approve(long transactionId, int status, string message)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@TransactionId", transactionId);
            adapter.AddParameter("@Status", status);
            adapter.AddParameter("@Message", message);
            adapter.ExecuteNonQuery("sp_TransactionLog_Approve", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void ApproveRegisterAgain(long transactionId, int status, string message)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@TransactionId", transactionId);
            adapter.AddParameter("@Status", status);
            adapter.AddParameter("@Message", message);
            adapter.ExecuteNonQuery("sp_TransactionLog_ApproveAccountRegisterAgain", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void ApproveAccountReNew(long transactionId, int accountId, int subscriptionId, int status, string message)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@TransactionId", transactionId);
            adapter.AddParameter("@AccountId", accountId);
            adapter.AddParameter("@SubscriptionId", subscriptionId);
            adapter.AddParameter("@Status", status);
            adapter.AddParameter("@Message", message);
            adapter.ExecuteNonQuery("sp_TransactionLog_ApproveAccountReNew", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public TransactionLog GetById(long transactionId)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", transactionId);
            return adapter.GetList<TransactionLog>("sp_TransactionLog_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit).FirstOrDefault();
        }

        public List<TransactionLogExtend> GetByAccountId(int accountId)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@AccountId", accountId);
            return adapter.GetList<TransactionLogExtend>("sp_TransactionLog_Select_By_AccountId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public List<TransactionLogExtend> Search(string email, int? subscriptionId, int? typePay, int? status, int? partnerId, DateTime? fromDate, DateTime? toDate, int? typeTransaction, int pageIndex, int pageSize, out int totalRecord)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);            
            adapter.AddParameter("@Email", email);
            adapter.AddParameter("@SubscriptionId", subscriptionId);
            adapter.AddParameter("@TypePay", typePay);
            adapter.AddParameter("@Status", status);
            adapter.AddParameter("@PartnerId", partnerId);
            adapter.AddParameter("@FromDate", fromDate);
            adapter.AddParameter("@ToDate", toDate);
            adapter.AddParameter("@TypeTransaction", typeTransaction);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            var list = adapter.GetList<TransactionLogExtend>("sp_TransactionLog_Search", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            totalRecord = Convert.ToInt32(output.Value);
            return list;
        }

        public List<TransactionLogExtend> GetAllTransactionLogByAccountId(int accountId, int pageIndex, int pageSize, out int totalRecord)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@AccountId", accountId);            
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            var list = adapter.GetList<TransactionLogExtend>("sp_TransactionLog_Select_All_By_AccountId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            totalRecord = Convert.ToInt32(output.Value);
            return list;
        }

        public void Delete(long transactionId)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", transactionId);
            adapter.ExecuteNonQuery("sp_TransactionLog_Delete", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }
    }
}
